/**
 * @copyright SilverBiology, LLC
 * @author Michael Giddens
 * @website http://code.google.com/p/taxamatch-webservice/
*/

Ext.namespace('Taxamatch');

Taxamatch.Database = function(config) {
	this.proxy = new Ext.data.HttpProxy({
		api: {
				read: {
						url: 'resources/api/app.php?cmd=getDbList'//'databasedata.json?task=listUsers'
					,	method: 'GET'		
				}
			,	create: {
					url: 'resources/api/app.php?cmd=addDatabase'//'databasedata.json?task=addUser'
					, method: 'GET'
				}
			,	update: {
						url: 'resources/api/app.php?cmd=updateDatabase'//'databasedata.json?task=updateUser'
					,	method: 'GET'
				}
			,	destroy: {
						url: 'resources/api/app.php?cmd=deleteDatabase'//'databasedata.json?task=deleteUser'
					,	method: 'GET'
				}
		}
	});
			
	this.reader = new Ext.data.JsonReader({
				totalProperty: 'total'
			,	successProperty: 'success'
			,	idProperty: 'id'
			,	root: 'data'
			,	messageProperty: 'message'  
		}, [
				{name: 'id'}
			,	{name: 'name'}
			,	{name: 'dbType'}
			,	{name: 'server'}
			,	{name: 'port'}
			,	{name: 'userName'}	
			,	{name: 'pass'}
			,	{name: 'title'}
	]);
			
	this.writer = new Ext.data.JsonWriter({
			encode: true
		,	writeAllFields: true
	});
			
	this.store = new Ext.data.GroupingStore({
			id: 'user'
		,	proxy: this.proxy
		,	scope:this
		,	reader: this.reader
		,	writer: this.writer
		,	autoSave: false
		,	batch: true 
		,	remoteSort: false
	});
	
	this.dbTypeCombo = new Ext.form.ComboBox({
			store: this.store
		,	displayField: 'dbType'
		,	scope: this
		,	mode: 'local'
		,	typeAhead: true
		,	triggerAction: 'all'
		,	emptyText: 'Select a DB Type...'
	});
	var textField =  new Ext.form.TextField();
		
	Ext.apply(this, config, {
			title: 'Database'	
		,	enableColumnMove: false
		,	enableColumnHide: false
		,	store: this.store
		,	id: 'db'
		,	iconCls: 'icon-database'
		,	cls: 'no-dirty-mark'
		,	columns: [{
					header: "Title"
				,	dataIndex: 'title'
				,	width: 80
				,	sortable: true
				,	editor: textField
				,	allowBlank: false
			},{
					header: "Server"
				,	dataIndex: 'server'
				,	width: 80
				,	scope:this
				,	sortable: true
				,	editor: textField
				,	allowBlank: false
			},{
					header: "Port"
				,	dataIndex: 'port'
				,	width: 80
				,	scope:this
				,	sortable: true
				,	editor: textField
				,	allowBlank: false
			},{
					header: "DB Type"
				,	dataIndex: 'dbType'
				,	width: 80
				,	scope: this
				,	sortable: true
				,	editor: this.dbTypeCombo
				,	allowBlank: false
			},{
					header: "DB: Name"
				,	dataIndex: 'name'
				,	width: 80
				,	sortable: true
				,	editor: textField
				,	allowBlank: false
			},{
					header: "Username"
				,	dataIndex: 'userName'
				,	width: 100
				,	scope: this
				,	sortable: true
				,	editor: textField
				,	allowBlank: false
			},{
					header: "Pass"
				,	dataIndex: 'pass'
				,	width: 100
				,	scope: this				
				,	sortable: true
				,	editor: textField
			}]
		,	sm: new Ext.grid.RowSelectionModel({multiSelect:true})
		,	loadMask: true
		,	found: false
		,	tbar: [{
					text: 'Add'
				,	iconCls: 'icon-databaseAdd'
				,	handler: this.onAdd
				,	scope: this
			}, '-', {
					text: 'Delete'
				,	iconCls: 'icon-databaseDelete'
				,	handler: this.onDelete
				,	scope: this
			}, '-',{
					text: 'Save'
				,	iconCls: 'icon-databaseSave'
				,	handler: this.onSave
				,	scope: this
			}]
	})

	Taxamatch.Database.superclass.constructor.call(this, config);

};

Ext.extend(Taxamatch.Database, Taxamatch.ux.UserGrid, {
		onAdd: function(btn, ev) {
			var u = new this.store.recordType({
					'dbType':'MySQL'
				,	'server':'localhost'
				,	'port':'3306'
				,	'name':''
				,	'title': ''
				,	'userName':''	
				,	'pass':''
			});
			this.stopEditing();
			this.store.insert(0, u);
			this.startEditing(0, 0);
		}

	,	onDelete : function(btn, ev) {
			if(!this.getSelectionModel().getSelected()){
				Taxamatch.Notice.msg('Notice','Please select database first.');
				return;
			}
			var index = this.getStore().indexOfId(this.getSelectionModel().getSelected().id);
			var rec = this.store.getAt(index);
			Ext.MessageBox.confirm( "Confirm", "Are you sure you wish to remove " + rec.data.name + "?" , function(e) {
				if (e == 'yes') {
					this.store.remove(rec);
					this.onSave();
				}
			},this);
		}
	,	onSave : function(btn, ev) {
				Ext.each(this.store.data.items, function(item){
					if(Ext.isEmpty(item.data.name) || Ext.isEmpty(item.data.pass) || Ext.isEmpty(item.data.userName) || Ext.isEmpty(item.data.title)){
						this.found = true;
					}
				}, this);
				if(!this.found){
					this.store.save();
				//	this.store.reload();
				} else {
					Taxamatch.Notice.msg("Notice", "Please enter valid value");
					this.found = false;
				}
			}
	});

